package utility;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Vector;

public class Storage {
	private Vector<Task> listOfTasks;
	private Vector<Task> pastTasks;
	private String filePath;
	private String fileName;
	private String fileHistory;

	// constants
	private final String seperator = ";";
	private final String fileNameMain = "dBase.txt";
	private final String fileNameHistory = "history.txt";
	private final String folderName = "storage";
	private final String fileNameTestMain = "dBaseTest.txt";
	private final String fileNameTestHistory = "historyTest.txt";

	public Storage() {
		listOfTasks = new Vector<Task>();
		pastTasks = new Vector<Task>();
		filePath = getPath(folderName, fileName);
		fileName = fileNameMain;
		fileHistory = fileNameHistory;
	}

	public void setTestingEnvironment() {
		this.fileName = fileNameTestMain;
		this.fileHistory = fileNameTestHistory;
		this.filePath = getPath(folderName, fileNameTestMain);
	}

	private String getPath(String directoryName2, String fileName2) {
		File directory = new File(directoryName2);
		String path = new String("");
		try {
			path = directory.getAbsolutePath() + "\\" + fileName2;
		} catch (Exception e) {

		}
		return path;
	}

	private void readFile(File fread, int fileType)
			throws FileNotFoundException {
		String[] currLine;
		int index = 0;
		Scanner scannerRead = new Scanner(fread);
		while (scannerRead.hasNext()) {
			currLine = scannerRead.nextLine().split(seperator);
			if (fileType == 0)
				index++;
			else {
				assert fileType == 1;
				index--;
			}
			Task currTask = createTask(currLine, index);
			if (fileType == 0)
				listOfTasks.add(currTask);
			else {
				assert fileType == 1;
				pastTasks.add(currTask);
			}
		}
		scannerRead.close();
	}

	private Task createTask(String[] currLine, int index) {
		Task curr = new Task();
		curr.startDate = currLine[0];
		curr.startTime = currLine[1];
		curr.endDate = currLine[2];
		curr.endTime = currLine[3];
		curr.task = currLine[4];
		curr.index = index;
		curr.status = currLine[5];
		return curr;
	}

	public void writeFile(Vector<Task> updatedList) throws IOException {
		PrintWriter fileOut = new PrintWriter(filePath);
		for (int i = 0; i < updatedList.size(); i++) {
			String tempTask = parseTask(updatedList.elementAt(i));
			fileOut.println(tempTask);
		}
		fileOut.close();
	}

	private String parseTask(Task currTask) {
		String taskString = new String("");
		taskString += currTask.startDate + seperator;
		taskString += currTask.startTime + seperator;
		taskString += currTask.endDate + seperator;
		taskString += currTask.endTime + seperator;
		taskString += currTask.task + seperator;
		taskString += currTask.status;

		return taskString;
	}

	public Vector<Task> retrieveData() {
		try {
			readFile(new File(filePath), 0);
		} catch (FileNotFoundException e) {
			createDirectory();
			filePath = getPath(folderName, fileNameMain);
			return listOfTasks;
		}
		return listOfTasks;
	}

	private void createDirectory() {
		File tempF = new File(folderName);
		try {
			if (tempF.exists()) {
				return;
			}
			tempF.mkdir();
		} catch (Exception e) {
			System.out.println("Error: " + e.getMessage());
		}
	}

	public void writePastTasks(Vector<Task> pastTasks) {
		PrintWriter outToPast;
		try {
			outToPast = new PrintWriter(getPath(folderName, fileHistory));
			for (int i = 0; i < pastTasks.size(); i++) {
				String tempTask = parseTask(pastTasks.elementAt(i));
				outToPast.println(tempTask);
			}
			outToPast.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public Vector<Task> retrieveHistory() {
		try {
			readFile(new File(getPath(folderName, fileHistory)), 1);
		} catch (FileNotFoundException e) {
		}
		return pastTasks;
	}
}
